package jmine.tec.utils.reflection.fast.asm;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import jmine.tec.utils.reflection.fast.AbstractFastMethod;
import jmine.tec.utils.reflection.fast.FastMethod;

/**
 * Implementacao de {@link FastMethod}
 * 
 * @author takeshi
 */
public class FastMethodImpl extends AbstractFastMethod implements FastMethod {

    protected final GeneratedFastClassAccessor generated;

    protected final int index;

    /**
     * C'tor
     * 
     * @param index o indice do metodo
     * @param method o {@link Method}
     * @param gen o accessor
     */
    public FastMethodImpl(int index, Method method, GeneratedFastClassAccessor gen) {
        super(method);
        this.index = index;
        this.generated = gen;
    }

    /**
     * {@inheritDoc}
     */
    public Object invoke(Object target, Object... args) throws InvocationTargetException {
        try {
            return this.generated.invokeMethodMultipleArgs(this.index, target, args);
        } catch (RuntimeException e) {
            throw e;
        } catch (Error e) {
            throw e;
        } catch (Exception e) {
            throw new InvocationTargetException(e);
        }
    }

    /**
     * @return the generated
     */
    public final GeneratedFastClassAccessor getGenerated() {
        return this.generated;
    }

}
